/**
 * Created by dong on 15/12/18.
 * 注这个文件来自render项目到时候要抽到公共项目中
 */
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
let Memcached = require('memcached');
const DEFAULTTIMEOUT = 15 * 60 * 1000; //默认存储15分钟
class MemcacheService {
    constructor(memcacheConfig) {
        this.memcached = new Memcached(memcacheConfig.memcahceServer.split(" "), {
            timeout: memcacheConfig.timeout || 3000,
            poolSize: memcacheConfig.poolSize || 15
        });
        this.memcached.on('failure', function (details) {
            console.error("Server " + details.server + "went down due to: " + details.messages.join(''));
        });
        this.memcached.on('reconnecting', function (details) {
            console.warn("Total downtime caused by server " + details.server + " :" + details.totalDownTime + "ms");
        });
    }
    //存储数据
    set(key, value, timeout) {
        return new Promise((resolve, reject) => {
            this.memcached.set(key, value, timeout || DEFAULTTIMEOUT, (err) => {
                console.info('memcached set[', key, ']返回值:  err:', err);
                err ? reject() : resolve();
            });
        });
    }
    delete(key) {
        return new Promise((resolve, reject) => {
            this.memcached.del(key, function (err) {
                console.info('memcached del[', key, ']返回值:  err:', err);
                err ? reject() : resolve();
            });
        });
    }
    /**
     * 注如果无值,返回的是undefined,不是报错
     *
     * @param key
     * @returns {Promise}
     */
    get(key) {
        return new Promise((resolve, reject) => {
            this.memcached.get(key, function (err, data) {
                if (err) {
                    console.warn('memcached get[', key, ']返回值:报错 err:', err);
                    reject(err);
                }
                else {
                    resolve(data);
                }
            });
        });
    }
    /**
     * 获取文件,返回
     * @param keyArrays
     * @returns {Promise}
     */
    getMulti(keyArrays) {
        return new Promise((resolve, reject) => {
            this.memcached.getMulti(keyArrays, function (err, data) {
                if (err) {
                    console.warn('memcached get[', keyArrays, ']返回值:报错 err:', err);
                    reject(err);
                }
                else {
                    resolve(data);
                }
            });
        });
    }
}
exports.default = MemcacheService;
